package com.cloudstudy.tool;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 * @author xiluhua
 * @since 20210130
 */
public class LogTool {

    public static Long interval = 60 * 1000 * 5L;

    public static Map<Object, String> logLevelMap = new HashMap<Object, String>();

    public static void init(Map<Object, String> logLevelMap){
        LogTool.logLevelMap = logLevelMap;
    }

    //-------------------------------------------------------------------------
    public static <T> void debug(Class<T> clazz, String logStr, boolean isPrintLine) {
        org.slf4j.Logger slfLogger2 = LoggerFactory.getLogger(clazz);
        if (isPrint(clazz,1)) {
            if (isPrintLine) {
                slfLogger2.debug("===========================================================================\n"+logStr);
            }else {
                slfLogger2.debug(logStr);
            }
        }
    }

    private static <T> boolean isPrint(Class<T> clazz,int level){
        boolean isPrint = false;

        String className = clazz.toString().split(" ")[1];
        boolean flag = logLevelMap.get(className) == null;
        String levelString = String.valueOf(logLevelMap.get(className));
        if (flag || levelString.indexOf(String.valueOf(level)) > -1 ) {
            isPrint = true;
        }
        return isPrint;
    }

    public static <T> void debug(Class<T> clazz, String format, Object arg) {
        if (isPrint(clazz,1)) {
            org.slf4j.Logger slfLogger2 = LoggerFactory.getLogger(clazz);
            slfLogger2.debug(format, arg);
        }
    }

    public static <T> void info(Class<T> clazz, String format, Object arg) {
        if (isPrint(clazz,2)) {
            org.slf4j.Logger slfLogger2 = LoggerFactory.getLogger(clazz);
            slfLogger2.info(format, arg);
        }
    }

    public static <T> void warn(Class<T> clazz, String format, Object arg) {
        if (isPrint(clazz,3)) {
            org.slf4j.Logger slfLogger2 = LoggerFactory.getLogger(clazz);
            slfLogger2.warn(format, arg);
        }
    }

    public static <T> void error(Class<T> clazz, String format, Object arg2) {
        if (isPrint(clazz,4)) {
            org.slf4j.Logger slfLogger2 = LoggerFactory.getLogger(clazz);
            slfLogger2.error(format, arg2);
        }
    }
    //-------------------------------------------------------------------------

    public static <T> void debug(Class<T> clazz, String format, Object arg1, Object arg2) {
        if (isPrint(clazz,1)) {
            org.slf4j.Logger slfLogger2 = LoggerFactory.getLogger(clazz);
            slfLogger2.debug(format, arg1, arg2);
        }
    }

    public static <T> void info(Class<T> clazz, String format,  Object arg1, Object arg2) {
        if (isPrint(clazz,2)) {
            org.slf4j.Logger slfLogger2 = LoggerFactory.getLogger(clazz);
            slfLogger2.info(format, arg1, arg2);
        }
    }

    public static <T> void warn(Class<T> clazz, String format,  Object arg1, Object arg2) {
        if (isPrint(clazz,3)) {
            org.slf4j.Logger slfLogger2 = LoggerFactory.getLogger(clazz);
            slfLogger2.warn(format, arg1, arg2);
        }
    }

    public static <T> void error(Class<T> clazz, String format,  Object arg1, Object arg2) {
        if (isPrint(clazz,4)) {
            org.slf4j.Logger slfLogger2 = LoggerFactory.getLogger(clazz);
            slfLogger2.error(format, arg1, arg2);
        }
    }

    public static <T> void error(Class<T> clazz, Exception e) {
        org.slf4j.Logger slfLogger2 = LoggerFactory.getLogger(clazz);
        slfLogger2.error(getStackTrace(e));
    }
    //-------------------------------------------------------------------------

    public static <T> void debug(Class<T> clazz, String format, Object[] argArray) {
        if (isPrint(clazz,1)) {
            org.slf4j.Logger slfLogger2 = LoggerFactory.getLogger(clazz);
            slfLogger2.debug(format, argArray);
        }
    }

    public static <T> void debug(Class<T> clazz, String message) {
        if (isPrint(clazz,1)) {
            org.slf4j.Logger slfLogger2 = LoggerFactory.getLogger(clazz);
            slfLogger2.debug(message);
        }
    }

    //    public static <T> void info(Class<T> clazz, String format,  Object[] argArray) {
//        if (isPrint(clazz,2)) {
//            org.slf4j.Logger slfLogger2 = LoggerFactory.getLogger(clazz);
//            slfLogger2.info(format, argArray);
//        }
//    }
    public static <T> void info(Class<T> clazz, String format,  Object...argArray) {
        if (isPrint(clazz,2)) {
            org.slf4j.Logger slfLogger2 = LoggerFactory.getLogger(clazz);
            slfLogger2.info(format, argArray);
        }
    }
    public static <T> void info(Class<T> clazz, String message) {
        if (isPrint(clazz, 2)) {
            org.slf4j.Logger slfLogger2 = LoggerFactory.getLogger(clazz);
            slfLogger2.info(message);
        }
    }

    public static <T> void warn(Class<T> clazz, String format,  Object[] argArray) {
        if (isPrint(clazz,3)) {
            org.slf4j.Logger slfLogger2 = LoggerFactory.getLogger(clazz);
            slfLogger2.warn(format, argArray);
        }
    }

    public static <T> void warn(Class<T> clazz, String message) {
        if (isPrint(clazz,3)) {
            org.slf4j.Logger slfLogger2 = LoggerFactory.getLogger(clazz);
            slfLogger2.warn(message);
        }
    }

    public static <T> void warn(Class<T> clazz, Exception e) {
        if (isPrint(clazz,3)) {
            org.slf4j.Logger slfLogger2 = LoggerFactory.getLogger(clazz);
            slfLogger2.error(getExceptionStr(e));
        }
    }

    public static <T> void error(Class<T> clazz, String message) {
        if (isPrint(clazz,4)) {
            org.slf4j.Logger slfLogger2 = LoggerFactory.getLogger(clazz);
            slfLogger2.error(message);
        }
    }

    public static <T> void error(Class<T> clazz, String format,  Object[] argArray) {
        org.slf4j.Logger slfLogger2 = LoggerFactory.getLogger(clazz);
        slfLogger2.error(format, argArray);
    }
    //-------------------------------------------------------------------------

    public static String getExceptionStr(Exception e) {

        StringWriter stringWriter = new StringWriter();
        PrintWriter writer = new PrintWriter(stringWriter);
        e.printStackTrace(writer);
        StringBuffer buffer = stringWriter.getBuffer();
        return buffer.toString();
    }

    public static String getStackTrace(Throwable throwable) {
        StringWriter sw = new StringWriter();
        PrintWriter pw = new PrintWriter(sw);
        try {
            throwable.printStackTrace(pw);
            return sw.toString();
        } finally {
            pw.close();
        }
    }

    public static void main(String[] args) {
        String[] arr = {"1","2","3"};
        LogTool.info(LogTool.class, "Temperature set to {}. Old temperature was {}. ", 1, 2);
        LogTool.info(LogTool.class, "set to a: {} b: {} b: {}", arr);
        LogTool.info(LogTool.class, "set to a: {} b: {} b: {}", 900,200,300);
        try
        {
            int i = 0;
            int m = 10 / i;
        } catch (Exception e)
        {
            System.out.println(e.getMessage());
            System.out.println("------调皮的分割线------");
            LogTool.error(LogTool.class,e);
        }
    }
}

@Builder
@Data
@AllArgsConstructor
@NoArgsConstructor
class SwrLogLevel {
    private Long id;
    private String className;
    private Integer logLevel;
    private Integer status;
}
